
package com.hlkj.pay.controller.ad;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.ad.IAdRefundOrderAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.ad.AdRefundOrderDto;
import com.hlkj.pay.dto.ad.query.AdRefundOrderQueryDto;
import com.hlkj.pay.infrastructure.model.ad.AdInfoDO;
import com.hlkj.pay.infrastructure.model.ad.AdMediaDO;
import com.hlkj.pay.infrastructure.model.ad.AdRefundOrderApplyDO;
import com.hlkj.pay.infrastructure.model.ad.AdRefundOrderDO;
import com.hlkj.pay.service.ad.IAdInfoService;
import com.hlkj.pay.service.ad.IAdMediaService;
import com.hlkj.pay.service.ad.IAdRefundOrderService;
import com.hlkj.pay.vo.ad.req.AdOrderRefundApplyReq;
import com.hlkj.pay.vo.ad.req.AdOrderRefundApproveReq;
import com.hlkj.pay.vo.ad.req.AdRefundOrderPageQueryReq;
import com.hlkj.pay.vo.ad.resp.AdOrderRefundApplyResp;
import com.hlkj.pay.vo.ad.resp.AdRefundOrderResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/10/23 11:36
 */
@Tag(name = "广告订单退款管理")
@RestController
@RequestMapping("/v1/ad/refund/")
@RequiredArgsConstructor
@Slf4j
public class AdRefundOrderController extends BaseController {

    private final IAdRefundOrderService adRefundOrderService;

    private final IAdRefundOrderAppService adRefundOrderAppService;
    private final IAdInfoService adInfoService;
    private final IAdMediaService adMediaService;

    @Operation(summary = "退款订单查询")
    @GetMapping("/order/page")
    CommonResult<PageResult<AdRefundOrderResp>> queryRefundOrderPage(AdRefundOrderPageQueryReq adPayOrderPageQueryReq) {
        AdRefundOrderQueryDto adRefundOrderQueryDto = BeanUtil.copy(adPayOrderPageQueryReq, AdRefundOrderQueryDto.class);
        processQueryDate(adPayOrderPageQueryReq,adRefundOrderQueryDto);
        adRefundOrderQueryDto.setRefundOrderNo(null);
        adRefundOrderQueryDto.setLikeRefundOrderNo(adPayOrderPageQueryReq.getRefundOrderNo());
        PageResult<AdRefundOrderDto> page = adRefundOrderService.queryPage(adRefundOrderQueryDto);
        List<AdRefundOrderDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, cn.hutool.core.bean.BeanUtil.copyToList(list, AdRefundOrderResp.class));
    }

    @Operation(summary = "退款订单详情")
    @GetMapping("/order/detail")
    CommonResult<AdRefundOrderResp> queryRefundOrderDetail(@RequestParam("refundOrderNo") String refundOrderNo) {
        AdRefundOrderDO payOrderDO = adRefundOrderService.queryDetail(refundOrderNo);
        if (payOrderDO == null) {
            return CommonResult.success(null);
        }
        AdInfoDO adInfoDO = adInfoService.queryById(payOrderDO.getAdId());
        AdMediaDO adMediaDO = adMediaService.queryById(adInfoDO.getMediaId());
        AdRefundOrderResp adPayOrderResp = cn.hutool.core.bean.BeanUtil.copyProperties(payOrderDO, AdRefundOrderResp.class);
        adPayOrderResp.setName(adInfoDO.getName());
        adPayOrderResp.setAdType(adInfoDO.getType());
        adPayOrderResp.setMediaName(adMediaDO.getName());
        return CommonResult.success(adPayOrderResp);
    }
    @Operation(summary = "售后订单列表查询")
    @GetMapping("/apply/page")
    CommonResult<PageResult<AdOrderRefundApplyResp>> queryRefundApplyOrderPage(AdRefundOrderPageQueryReq adPayOrderPageQueryReq) {
        AdRefundOrderQueryDto adRefundOrderQueryDto = BeanUtil.copy(adPayOrderPageQueryReq, AdRefundOrderQueryDto.class);
        processQueryDate(adPayOrderPageQueryReq,adRefundOrderQueryDto);
        adRefundOrderQueryDto.setInsOrderNo(null);
        adRefundOrderQueryDto.setLikeInsOrderNo(adPayOrderPageQueryReq.getInsOrderNo());
        adRefundOrderQueryDto.setPhone(null);
        adRefundOrderQueryDto.setLikePhone(adPayOrderPageQueryReq.getPhone());
        PageResult<AdRefundOrderApplyDO> page = adRefundOrderService.queryApplyPage(adRefundOrderQueryDto);
        List<AdRefundOrderApplyDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, cn.hutool.core.bean.BeanUtil.copyToList(list, AdOrderRefundApplyResp.class));
    }

    @Operation(summary = "售后订单详情")
    @GetMapping("/apply/detail")
    CommonResult<AdOrderRefundApplyResp> queryRefundApplyOrderDetail(@RequestParam("refundApplyNo") String refundApplyNo) {
        AdRefundOrderApplyDO payOrderDO = adRefundOrderService.queryByRefundApplyNo(refundApplyNo);
        if (payOrderDO == null) {
            return CommonResult.success(null);
        }
        AdOrderRefundApplyResp adPayOrderResp = cn.hutool.core.bean.BeanUtil.copyProperties(payOrderDO, AdOrderRefundApplyResp.class);
        return CommonResult.success(adPayOrderResp);
    }

    @Operation(summary = "售后订单申请")
    @PostMapping("/order/apply")
    CommonResult<Void> applyRefundOrder(@RequestBody AdOrderRefundApplyReq adOrderRefundApplyReq) {
        return adRefundOrderAppService.applyRefundOrder(adOrderRefundApplyReq);
    }
    @Operation(summary = "售后订单审批")
    @PostMapping("/order/approve")
    CommonResult<Void> approveRefundOrder(@RequestBody AdOrderRefundApproveReq adOrderRefundApproveReq) {
        return adRefundOrderAppService.approveRefundOrder(adOrderRefundApproveReq);
    }
    
    @Operation(summary = "一键退款")
    @PostMapping("/order/create")
    CommonResult<Void> refundOrder(@Validated @RequestBody AdOrderRefundApplyReq adOrderRefundApplyReq) {
        return adRefundOrderAppService.createRefundOrder(adOrderRefundApplyReq);
    }
}
